package com.meihua.shellapp.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.FileProvider;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.meihua.shellapp.R;
import com.meihua.shellapp.base.MyApplication;
import com.meihua.shellapp.utils.ConstantUtils;
import com.meihua.shellapp.utils.FileUtil;

import java.io.File;

/**
 * 作者：lc on 2017/10/18.
 * 邮箱：lc0607live@163.com
 */

public class CheckPhotoDialog extends Dialog implements View.OnClickListener {

    private TextView dialog_select_camera_txt;//拍照
    private TextView dialog_select_pic_txt;//相册
    private TextView dialog_select_cancle_txt;//取消
    private Activity activity;


    public CheckPhotoDialog(@NonNull Activity context) {
        super(context, R.style.MyDialog);
        this.activity = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_check_photo);
        setCanceledOnTouchOutside(true);
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = ConstantUtils.getInstance().getScreenW(getContext());
        params.gravity = Gravity.BOTTOM;
        window.setAttributes(params);

        initView();
    }

    private void initView() {
        dialog_select_camera_txt = findViewById(R.id.tv_camera);
        dialog_select_pic_txt = findViewById(R.id.tv_album);
        dialog_select_cancle_txt = findViewById(R.id.tv_cencle);

        dialog_select_camera_txt.setOnClickListener(this);
        dialog_select_pic_txt.setOnClickListener(this);
        dialog_select_cancle_txt.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_camera://拍照
                //第二个参数是需要申请的权限
                if (Build.VERSION.SDK_INT > 22 && (ContextCompat.checkSelfPermission(activity,
                        android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED
                        || ContextCompat.checkSelfPermission(activity,
                        android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)) {
                    //先判断有没有权限 ，没有就在这里进行权限的申请
                    ActivityCompat.requestPermissions(activity,
                            new String[]{android.Manifest.permission.CAMERA, android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 123);
                    dismiss();
                } else {

                    MyApplication.getInstance().time = System.currentTimeMillis();

                    //MediaStore.ACTION_IMAGE_CAPTURE  调用系统的照相机
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    Uri imageUri;
                    File file = new File(FileUtil.getUtil().SDPATH, "user_photo" + MyApplication.getInstance().time + ".png");
                    file.getParentFile().mkdirs();
                    if (Build.VERSION.SDK_INT > 23) {
                        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                        imageUri = FileProvider.getUriForFile(getContext(), "com.meihua.shellapp.fileprovider", file);
                    } else {
                        imageUri = Uri.fromFile(file);
                    }
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                    activity.startActivityForResult(intent, 1);//1
                    dismiss();
                }
                break;
            case R.id.tv_album://相册
                if (Build.VERSION.SDK_INT > 22 && ContextCompat.checkSelfPermission(activity,
                        android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                    //先判断有没有权限 ，没有就在这里进行权限的申请
                    ActivityCompat.requestPermissions(activity,
                            new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 123);
                    dismiss();

                } else {
                    Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    activity.startActivityForResult(i, 0);//0
                    dismiss();
                }
                break;
            case R.id.tv_cencle://取消
                dismiss();
                break;
        }
    }
}
